﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Data.OleDb;

public partial class EmployeesSalary_EmpSalaryUpLoad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //object[] obj = new object[] { null };

        //object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "EmpSalaryUpload", obj);
   
        MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
        master.HelpHref = "";
        master.PageName = "薪资单上传";
        master.PicUrl = "";
        master.IntiBar();



        ((HiddenField)EmpSalaryListUpDown.FindControl("HoverId")).Value = "ListUpload";
    }

    protected void UpLoad_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUp.HasFile)
            {
                string[] FileName = FileUp.FileName.Split('.');

                string FileType = FileName[1];

                if (FileType != "xls")
                {
                    throw new Exception("上传文件类型错误！");

                }


                string path = MapPath("~/UploadFile/Salary/");
                string[] time = txtYM.Text.Split('-');

                int year = Convert.ToInt32(time[0]);
                int month = Convert.ToInt32(time[1]);

                if (!new DirectoryInfo(path + year).Exists)
                {
                    Directory.CreateDirectory(path + year);
                }

                if (!new DirectoryInfo(path + year + @"\" + month).Exists)
                {
                    Directory.CreateDirectory(path + year + @"\" + month);
                }
                string FilePath = path + year + @"\" + month + @"\" + DateTime.Now.ToFileTime() + @"." + FileType;

                FileUp.SaveAs(FilePath);

                DataSet ds = ExecleDs(FilePath, SheetName.Text, year, month, Convert.ToInt32(Srvtools.CliUtils.GetValue("_UserCode")[1]),DateTime.Now);

                object[] obj = new object[] { ds.Tables[0] };

                object[] ret = Srvtools.CliUtils.CallMethod("SEmployees", "EmpSalaryUpload", obj);

                if ((bool)ret[1])
                {
                    ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('导入成功！')", true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('导入失败！')", true);
                    errorMsg.InnerHtml = "失败原因：<br/>" + ret[2].ToString();
                }
            }
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('导入失败！')", true);
            errorMsg.InnerHtml = "失败原因：<br/>" + ex.Message;
            //ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ex.Message + "')", true);
        }
    }


    public DataSet ExecleDs(string filenameurl, string Sheet, int years, int months, int editid, DateTime nowtime)
    {
        string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
        OleDbConnection conn = new OleDbConnection(strConn);
        conn.Open();
        DataSet ds = new DataSet();
        OleDbDataAdapter odda = new OleDbDataAdapter("select 人员编号,'" + years + "','" + months + "','" + editid + "','" + nowtime + "',0,标准月薪,岗位津贴,各类补贴,计件工资1,非计件收入,补发工资,加班小时,加班工资,中班天数,中班补贴,加班费,奖金,长期服务奖,离职补偿金,应发合计,实际应发,病假小时,病假扣款1,病假扣款2,事假小时,事假扣款1,事假扣款2,养老金,医保金,待业金,公积金,质量扣款,其他扣款,代扣税,扣款合计,纪律扣款,实发合计,应税工资,四金计缴基数,月薪,四金标志,年终奖,年终奖代扣税,工资代扣税,扣税合计,姓名,税后扣款 from [" + Sheet + "$] where 姓名 is not null ", conn);
        odda.Fill(ds);
        return ds;
    }


   


}
